<template>
  <div class="container">
    <el-table
        :data="tableData"
        style="width: 100%">
      <el-table-column
          prop="id"
          label="#"
          align="center">
      </el-table-column>
      <el-table-column
          label="城市"
          align="center">
        <template slot-scope="scope">
          <div @click="toIdDetail(scope.row.cityId)" style="color: #007aff">
            {{ scope.row.city }}
          </div>
        </template>
      </el-table-column>
      <el-table-column
          prop="AQI"
          label="AQI"
          align="center">
      </el-table-column>
      <el-table-column
          prop="level"
          label="等级"
          align="center">
      </el-table-column>
      <el-table-column
          label="空气等级"
          align="center">
        <template slot-scope="scope">
          <div :style="scope.row.backgroundColor">
            <span style="color: white;font-weight: bold;">{{ scope.row.airType }}</span>
          </div>
        </template>
      </el-table-column>
    </el-table>
  </div>
</template>

<script>
import {mapState} from "vuex";

export default {
  name: 'Rank',
  data() {
    return {
      tableData: [],
      //动态绑定颜色
      backColor: {}
    }
  },
  mounted() {
    this.getShowAir()
  },
  methods: {
    //跳转到详细信息
    async toIdDetail(cityId) {
      await this.$store.dispatch("detail/getIdCityList", cityId);
      this.$router.push("/detail/" + cityId);
    },

    // 得到空气数据
    async getShowAir() {
      let a = 1;
      await this.$store.dispatch("rank/getShowAir");
      // console.log(this.airInfo);
      this.airInfo.map((data) => {
        this.tableData.push({
          airType: data.airTypeId,
          AQI: data.aqi,
          city: data.cityName,
          level: data.level,
          color: data.color,
          id: a++,
          backgroundColor: "background-color: " + data.color
        });
      });
    },
  },
  computed: {
    //获取空气排行信息
    ...mapState("rank", ["airInfo"]),
  }

}
</script>

<style lang="less" scoped>
.container {
  display: flex;
  width: 1000px;
  margin-right: auto;
  margin-left: auto;
  margin-top: 20px;
  border-radius: 4px;
  box-shadow: 0 2px 20px 0 rgba(0, 0, 0, 0.08);
  background-color: #fff;
  padding: 24px;
}
</style>